package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 用户中心
 * 修改，浏览用户信息
 */
@WebServlet("/usercenter")
public class UserCenterServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		try {
			openid=DES3Util.decrypt(openid);
		} catch (Exception e) {
			log(e.getMessage());
		}
//		openid=CookieUtil.decodeCookie(openid);
		if (null!=openid && openid!="") {
			WxUtils wxu=new WxUtils();
			TokenUtils to=new TokenUtils();
			JSONObject json=new JSONObject();
			String accesstoken=to.getAccesstoken();
			int changecounts=to.CountProductByOpenid(openid);
			JSONObject jsonuser=JSONObject.fromObject(wxu.getUserinfo(accesstoken, openid));
			if(jsonuser.containsKey("errcode")){
				System.out.println("******AccessToken失效,重新获取******");
				JSONObject jsontoken=JSONObject.fromObject(wxu.getAccessToken());
				if (null!=jsontoken) {
					//更新数据库accesstoken
					to.updateAccessToken(jsontoken.get("access_token").toString());
					accesstoken=jsontoken.get("access_token").toString();
					JSONObject jsonuseragain=JSONObject.fromObject(wxu.getUserinfo(accesstoken, openid));
					String nickname=(String)jsonuseragain.get("nickname");
					String headimg=(String)jsonuseragain.get("headimgurl");
					int subscribetime=(int)jsonuseragain.get("subscribe_time");
					nickname=nickname.replaceAll("[\\x{10000}-\\x{10FFFF}]", "");
					Map user=to.getFyUserinfoMap(openid);
					to.UpdateNicknameHeadimg(nickname, headimg, openid,changeTime(String.valueOf(subscribetime)));
					json.put("result", 0);
					json.put("changecounts", changecounts);
					json.put("nickname", nickname);
					json.put("headimg", headimg);
					json.put("name", user.get("name").toString());
					json.put("points", user.get("points").toString());
					json.put("school", user.get("school").toString());
					json.put("building", user.get("building").toString());
					json.put("grade", user.get("grade").toString());
					json.put("major", user.get("major").toString());
					json.put("mobile", user.get("mobile").toString());
					ResponseUtils.renderJson(response, json.toString());
				}else{
					json.put("result", 1);
					ResponseUtils.renderJson(response, json.toString());
				}
			}else{
				String nickname=(String)jsonuser.get("nickname");
				String headimg=(String)jsonuser.get("headimgurl");
				int subscribetime=(int)jsonuser.get("subscribe_time");
				nickname=nickname.replaceAll("[\\x{10000}-\\x{10FFFF}]", "");
				Map user=to.getFyUserinfoMap(openid);
				to.UpdateNicknameHeadimg(nickname, headimg, openid,changeTime(String.valueOf(subscribetime)));
				json.put("result", 0);
				json.put("changecounts", changecounts);
				json.put("nickname", nickname);
				json.put("headimg", headimg);
				json.put("name", user.get("name").toString());
				json.put("points", user.get("points").toString());
				json.put("school", user.get("school").toString());
				json.put("building", user.get("building").toString());
				json.put("grade", user.get("grade").toString());
				json.put("major", user.get("major").toString());
				json.put("mobile", user.get("mobile").toString());
				ResponseUtils.renderJson(response, json.toString());
			}
		}else{
			JSONObject json=new JSONObject();
			json.put("result", 2);
			ResponseUtils.renderJson(response, json.toString());
		}
	}
	
	public static String changeTime(String time){
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		long ntime=new Long(time);  
		String d = format.format(ntime*1000);
		return d;
	}

}
